import { useMemo, useRef, useState } from 'react';
import {Button, Form,Input, Toast} from 'antd-mobile';
import { useRequest } from 'ahooks';
import { putManagerAccountCreateOrUpdate } from '@/api/manager';
interface Props {
    data?:API.AccountDto
    onOk: ()=>void;
}
export default function AddOrUpdateAccount({
    onOk,data:{
        currentBoarding:boardingInfo,...rest} = {}
    }:Props){
    const [initValue] = useState(()=>{
        const result:Record<string,any> = {
            baiscInfo:{
                email:rest?.email,
                password: rest?.password || 'Cqx850727.'
            }
        }
        if(boardingInfo){
            result.boardingInfo = {
                ...boardingInfo,
                customName: boardingInfo?.last_Name && boardingInfo?.first_Name ? `${boardingInfo?.last_Name} ${boardingInfo?.first_Name}` : '',
                mobile: boardingInfo?.phone_Mobile,
                flightNo: boardingInfo?.airlinesNo ? `${boardingInfo?.airlinesNo}${boardingInfo?.flightNo}` : ''
            }
        }
        return result;
    });
    const [loading,setLoading] = useState(false);
    const required = useMemo(()=>{
        if (!boardingInfo) return false;
        return Object.values(boardingInfo).some(v => v !== undefined && v !== null && v !== '');
    },[boardingInfo]);

    const onFinish = async (values: any) => {
         try{
            setLoading(true);
            const requestBody = {...values};
            const bValues = Object.values(requestBody.boardingInfo).filter(Boolean);
            if(bValues.length === 0){
                requestBody.boardingInfo = null;
            }
            await putManagerAccountCreateOrUpdate(requestBody);
            setLoading(false);
            onOk();
         }
         catch(e:any){
            setLoading(false);
            Toast.show({
                icon: 'fail',
                content: e?.message || '保存失败',
            });
            console.error('保存用户信息出错',e);
         }
    }
    return(
        <Form
            layout='horizontal'
            footer={<Button block type='submit' color='primary' size='large' loading={loading}>提交</Button>}
            onFinish={onFinish}
            initialValues={initValue}
            mode='card'
        >
            <Form.Item
                name={['baiscInfo','email']}
                label='邮箱'
                rules={[
                    { required: true, message: '邮箱不能为空' },
                    { type:'email',message:'邮箱格式不正确' }
                ]}
                disabled={!!rest.email}
            >
                <Input />
            </Form.Item>
            <Form.Item
                name={['baiscInfo','password']}
                label='密码'
                rules={[{ required: true, message: '请输入密码' }]}
            >
                <Input />
            </Form.Item>
            <Form.Item
                name={['boardingInfo','customName']}
                label='姓名'
                rules={[{ required: required, message: '请输入姓名' }]}
            >
                <Input placeholder='支持中文和拼音模式(自己敲一个空格)' clearable={true} />
            </Form.Item>
            <Form.Item
                name={['boardingInfo','mobile']}
                label='手机号'
                rules={[
                    { required: required, message: '请输入手机号' },
                    { pattern: /^1[3-9]\d{9}$/, message: '请输入有效的中国大陆手机号' }
                ]}
            >
                <Input clearable={true}/>
            </Form.Item>
            <Form.Item
                name={['boardingInfo','birthday']}
                label='生日'
                rules={[
                    { required: required, message: '请输入生日' },
                    { pattern: /^\d{4}\/\d{2}\/\d{2}$/, message: '生日格式应为: 2025/06/27' }
                ]}
            >
                <Input placeholder='格式:2025/06/27' clearable={true} />
            </Form.Item>
            <Form.Item
                name={['boardingInfo','flightNo']}
                label='航班号'
                rules={[
                    { required: required, message: '请输入航班号' },
                    { pattern: /^[A-Z0-9]{2}\d{4}$/i, message: '航班号格式应为: 3U0242' }
                ]}
            >
                <Input placeholder='格式:3U0242' clearable={true} />
            </Form.Item>
            <Form.Item
                name={['boardingInfo','departureDate']}
                label='出发日期'
                rules={[
                    { required: required, message: '请输入出发日期' },
                    { pattern: /^\d{4}\/\d{2}\/\d{2}$/, message: '出发日期格式应为: 2025/06/27' }
                ]}
            >
                <Input placeholder='格式:2025/06/27' clearable={true} />
            </Form.Item>
            <Form.Item
                name={['boardingInfo','departureTime']}
                label='出发时间'
                rules={[
                    { required: required, message: '请输入出发日期' },
                    { pattern: /^([01]\d|2[0-3]):([0-5]\d)$/, message: '出发时间格式应为: 22:00' }
                ]}
            >
                <Input placeholder='格式:22:00' clearable={true}/>
            </Form.Item>
        </Form>
    )
}